package cn.lili.modules.payment.entity.dto;

import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.order.order.entity.dos.OrderFlow;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 分佣积分列表
 */
@Data
@Builder
public class OutPointOrderItem {


    @ApiModelProperty(value = "昵称")
    private String nickname;

    @ApiModelProperty(value = "用户ID")
    private String userId;

    @ApiModelProperty(value = "分账接收方类型")
    private SceneEnums scene;

    @ApiModelProperty(value = "分账金额")
    private Double price;

    @ApiModelProperty(value = "订单号")
    private String orderSn;


    // 根据订单获取所有待分账用户
    public static List<OutPointOrderItem> generateItems(OrderFlow orderFlow) {

        System.out.println(orderFlow);

        List<OutPointOrderItem> outOrderItems = new ArrayList<>();

        Double sum = 0D;


        if (orderFlow.getMemberGreenScore() != null && orderFlow.getMemberGreenScore() > 0) {
            outOrderItems.add(OutPointOrderItem.builder().scene(SceneEnums.MEMBER)
                    .orderSn(orderFlow.getOrderSn())
                    .nickname(orderFlow.getNickname()).price(orderFlow.getMemberGreenScore()).userId(orderFlow.getMemberId()).build());
            sum = CurrencyUtil.add(sum , orderFlow.getMemberGreenScore());
        }

        if (orderFlow.getDistributionId() != null && orderFlow.getDistributionSettlementPrice() != null && orderFlow.getDistributionSettlementPrice() > 0) {
            outOrderItems.add(OutPointOrderItem.builder().scene(SceneEnums.MEMBER)
                    .orderSn(orderFlow.getOrderSn())
                    .nickname(orderFlow.getDistributionName()).price(orderFlow.getDistributionSettlementPrice()).userId(orderFlow.getDistributionId()).build());
        }


        if (orderFlow.getStoreId() != null && orderFlow.getStoreGreenScore() != null && orderFlow.getStoreGreenScore() > 0) {
            outOrderItems.add(OutPointOrderItem.builder()
                    .scene(SceneEnums.STORE).nickname(orderFlow.getStoreName()).price(orderFlow.getStoreGreenScore())
                    .orderSn(orderFlow.getOrderSn())
                    .userId(orderFlow.getStoreId()).build());
            sum = CurrencyUtil.add(sum , orderFlow.getStoreGreenScore());
        }


        if (orderFlow.getSupplierId() != null && !orderFlow.getSupplierId().equals("-1") && orderFlow.getSupplierGreenScore() != null && orderFlow.getSupplierGreenScore() > 0) {
            outOrderItems.add(OutPointOrderItem.builder()
                    .orderSn(orderFlow.getOrderSn())
                    .scene(SceneEnums.SUPPLIER).nickname(orderFlow.getSupplierName()).price(orderFlow.getSupplierGreenScore()).userId(orderFlow.getSupplierId()).build());

            sum = CurrencyUtil.add(sum , orderFlow.getSupplierGreenScore());
        }


        // 城市站点补充两个
        if (orderFlow.getUrbanIdBySupplier() != null && orderFlow.getSiteGreenScore() != null && orderFlow.getSiteGreenScore() > 0) {
            outOrderItems.add(OutPointOrderItem.builder()
                    .orderSn(orderFlow.getOrderSn())
                    .scene(SceneEnums.URBAN_SUBSTATION).nickname(orderFlow.getUrbanNameBySupplier()).price(orderFlow.getSiteGreenScore()).userId(orderFlow.getUrbanIdBySupplier()).build());

            sum = CurrencyUtil.add(sum , orderFlow.getSiteGreenScore());
        }

        if (orderFlow.getUrbanIdByAddress() != null && orderFlow.getSiteAddressGreenScore() != null && orderFlow.getSiteAddressGreenScore() > 0) {
            outOrderItems.add(OutPointOrderItem.builder()
                    .orderSn(orderFlow.getOrderSn())
                    .scene(SceneEnums.URBAN_SUBSTATION).nickname(orderFlow.getUrbanNameByAddress()).price(orderFlow.getSiteAddressGreenScore()).userId(orderFlow.getUrbanIdByAddress()).build());

            sum = CurrencyUtil.add(sum , orderFlow.getSiteAddressGreenScore());
        }

        if (orderFlow.getLowerCommissId() != null && orderFlow.getLowerCommiss() != null && orderFlow.getLowerCommiss() > 0) {
            outOrderItems.add(OutPointOrderItem.builder()
                    .orderSn(orderFlow.getOrderSn())
                    .scene(SceneEnums.LOWER)
                    .price(orderFlow.getLowerCommiss())
                    .userId(orderFlow.getLowerCommissId())
                    .build());
            sum = CurrencyUtil.add(sum , orderFlow.getLowerCommiss());
        }

        if (orderFlow.getLowerSubCommissId() != null && orderFlow.getLowerSubCommiss() != null && orderFlow.getLowerSubCommiss() > 0) {
            outOrderItems.add(OutPointOrderItem.builder()
                    .orderSn(orderFlow.getOrderSn())
                    .scene(SceneEnums.LOWERSUB)
                    .price(orderFlow.getLowerSubCommiss())
                    .userId(orderFlow.getLowerSubCommissId())
                    .build());
            sum = CurrencyUtil.add(sum , orderFlow.getLowerCommiss());
        }

        if (StringUtils.isNotEmpty(orderFlow.getOcUserId()) && orderFlow.getUnionGreenScore() > 0) {
            outOrderItems.add(OutPointOrderItem.builder()
                    .orderSn(orderFlow.getOrderSn())
                    .scene(SceneEnums.OPERATION_CENTER)
                    .price(orderFlow.getUnionGreenScore())
                    .userId(orderFlow.getOcUserId())
                    .build());
            sum = CurrencyUtil.add(sum , orderFlow.getUnionGreenScore());
        }

        outOrderItems.add(OutPointOrderItem.builder().userId("-1").nickname("平台佣金").scene(SceneEnums.SYSTEM)
                .orderSn(orderFlow.getOrderSn())
                .price(CurrencyUtil.sub(orderFlow.getGreenScore(), CurrencyUtil.sub(CurrencyUtil.div(orderFlow.getFmoney()==null ? 0 : orderFlow.getFmoney(), 10), sum))).build());


        return outOrderItems;
    }


    public String generateDescription() {
        return switch (this.scene) {
            case MEMBER -> String.format("[%s]推广获利-订单[%s]-分账[%s元]", this.nickname, this.orderSn, StringUtils.formatPrice(this.price));
            case STORE -> String.format("[%s]店铺-订单[%s]-分账[%s元]", this.nickname, this.orderSn, StringUtils.formatPrice(this.price));
            case SUPPLIER -> String.format("[%s]供应商-订单[%s]-分账[%s元]", this.nickname, this.orderSn, StringUtils.formatPrice(this.price));
            case SYSTEM -> String.format("[%s]-订单[%s]-分账[%s元]", this.nickname, this.orderSn, StringUtils.formatPrice(this.price));
            default -> "";
        };
    }

    public String generateReduceDescription() {
        return "平台余额扣除:" + generateDescription();
    }

}
